package com.tongji.seller.view.popup
{
	import com.tongji.seller.events.DelStoreSceneEvent;
	import com.tongji.seller.events.GetStoreSavedSceneEvent;
	import com.tongji.seller.events.utilsEvent.BubblesEvent;
	import com.tongji.seller.events.utilsEvent.ChangeStepEvent;
	import com.tongji.seller.model.StoreSceneModel;
	import com.tongji.seller.model.UserModel;
	import com.tongji.seller.model.UserSceneModel;
	import com.tongji.seller.utils.PageTools;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 *
	 * @author caddy
	 * @date 2011-12-8
	 */
	public class SavedSceneTitleWinMediator extends Mediator
	{
		[Inject]
		public var view:SavedSceneTitleWindow;
		[Inject]
		public var userModel:UserModel;
		
		[Inject]
		public var userSceneModel:StoreSceneModel;
		override public function onRegister():void
		{
			BindingUtils.bindProperty(view.savedSceneList, "dataProvider", userSceneModel, "matcharray");

			eventMap.mapListener(view, CloseEvent.CLOSE, closeHandler, CloseEvent);
			eventMap.mapListener(view.saveBtn, MouseEvent.CLICK, saveSceneHandler, MouseEvent);
			
			addViewListener(BubblesEvent.EDIT_MY_SCENE, editMySceneHandler, BubblesEvent);
			addViewListener(BubblesEvent.DELET_MY_SCENE, deletMySceneHandler, BubblesEvent);
			
			var page:PageTools = new PageTools(userSceneModel.sceneCount, 10, 15, userSceneModel.pageIndex);
			view.pageBar.createDataProvider([page.nums, page.pageArray, page.pageCount, userSceneModel.pageIndex]);
			addContextListener(GetStoreSavedSceneEvent.GET_SAVED_SCENE, createPageBarHandler, GetStoreSavedSceneEvent);
		}
		
		private function createPageBarHandler(event:GetStoreSavedSceneEvent):void
		{
			var page:PageTools = new PageTools(userSceneModel.sceneCount, 10, 15, userSceneModel.pageIndex);
			view.pageBar.createDataProvider([page.nums, page.pageArray, page.pageCount, userSceneModel.pageIndex]);	
		}
		
		private function editMySceneHandler(event:BubblesEvent):void
		{
			dispatch(new ChangeStepEvent(ChangeStepEvent.TO_THIRD_STEP));
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
		}
		private function deletMySceneHandler(event:BubblesEvent):void
		{
			
			dispatch(new DelStoreSceneEvent(DelStoreSceneEvent.DEL_STORE_USER_SCENE, userModel.currentUserID, event.object.sid));
		}
		private function saveSceneHandler(event:MouseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
		}
		
		private function closeHandler(event:CloseEvent):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediatorByView(view);
		}
	}
}